import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CounterWidget extends StatefulWidget {
  const CounterWidget({required Key key, this.initValue: 0});

  final int initValue;

  @override
  _CounterWidgetState createState() => new _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    //初始化状态
    _counter = widget.initValue;
    print("initState");
  }

  @override
  Widget build(BuildContext context) {
    print("build");
    return Scaffold(
      body: Center(
        child: Column(children: <Widget>[
          FlatButton(
            child: Text('$_counter'),
            //点击后计数器自增
            onPressed: () => setState(
              () => ++_counter,
            ),
          ),
          Builder(
            builder: (BuildContext context) {
            return  ElevatedButton(
                onPressed: () {
                  ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                      content: Text('A SnackBar has been shown.'),
                    ),
                  );

                  // ScaffoldState _state = Scaffold.of(context);
                  // _state.showSnackBar(
                  //   const SnackBar(
                  //     content: Text('A SnackBar has been shown.'),
                  //   ),
                  // );
                },
                child: Text("显示SnackBar"),
              );
            },
          ),
          CupertinoButton(
              color: CupertinoColors.activeBlue,
              child: Text("Cupertino风格按钮"),
              onPressed: () {}
          ),
        ]),

      ),
    );
  }

  @override
  void didUpdateWidget(CounterWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    print("didUpdateWidget");
  }

  @override
  void deactivate() {
    super.deactivate();
    print("deactive");
  }

  @override
  void dispose() {
    super.dispose();
    print("dispose");
  }

  @override
  void reassemble() {
    super.reassemble();
    print("reassemble");
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print("didChangeDependencies");
  }
}
